﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using TjZheng.CMS.Process;
using TjZheng.Controls;
using TjZheng.Framework;
using TjZheng.Power.Business;
using TjZheng.Power.DataEntity;

namespace TjZheng.CMS.Controllers
{
    public class IconInfoController : CMSController
    {
        BizIconInfo objBizIconInfo { get; set; }
        public IconInfoController()
        {
            objBizIconInfo = new BizIconInfo();
        }

        public ActionResult Index()
        {
            return View();
        }

        //[QueryFilter]
        [QueryMsSqlFilter]
        [AuthorizeFilter(PowerType.CheckLogin)]
        public ActionResult ListJson(int page, int rows)
        {
            //return Json(objBizIconInfo.GetPageResult(page - 1, rows, QueryResult.Condition, "Id")); 
            return Json(objBizIconInfo.GetPageResult(page - 1, rows, QueryResult.Condition, QueryResult.Parameters, "SortIndex asc"));
        }

        public ActionResult EditPage(long? id)
        {
            IconInfo objIconInfo = null;
            if (id.HasValue)
            {
                objIconInfo = objBizIconInfo.GetRecord(id.Value);
            }
            else
            {
                objIconInfo = new IconInfo();
            }
            return View(objIconInfo);
        }

        //[ValidateAntiForgeryToken]
        [ValidateInput(false)]
        public ActionResult Save(IconInfo objProvide)
        {
            InvokeResult objInvokeResult = new InvokeResult();
            if (objProvide == null)
            {
                objInvokeResult.ResultCode = "ProvideNull";
                objInvokeResult.ResultMessage = "保存失败";
                return Json(objInvokeResult, "text/plain");
            }
            IconInfo objTarget = null;
            if (objProvide.Id <= 0)
            {
                objTarget = new IconInfo();
                Save(objTarget, objProvide);
                objTarget.Id = objBizIconInfo.Add(objTarget);
                objTarget.SortIndex = objTarget.Id;
                objBizIconInfo.Update(objTarget);
            }
            else
            {
                objTarget = objBizIconInfo.GetRecord(objProvide.Id);
                if (objTarget == null)
                {
                    objInvokeResult.ResultCode = "DataNull";
                    objInvokeResult.ResultMessage = "保存失败";
                    return Json(objInvokeResult, "text/plain");
                }
                Save(objTarget, objProvide);
                objBizIconInfo.Update(objTarget);
            }

            objInvokeResult.ResultCode = "Success";
            objInvokeResult.ResultMessage = "保存成功";
            return Json(objInvokeResult, "text/plain");
        }

        public ActionResult Delete(string id)
        {
            InvokeResult objInvokeResult = new InvokeResult();
            if (id.IsNull())
            {
                objInvokeResult.ResultCode = "IDNull";
                objInvokeResult.ResultMessage = "请先选择一条数据";
                return Json(objInvokeResult, "text/plain");
            }
            objBizIconInfo.DeleteIDString(id);
            objInvokeResult.ResultCode = "Success";
            objInvokeResult.ResultMessage = "删除成功";
            return Json(objInvokeResult, "text/plain");
        }

        [AuthorizeFilter(PowerType.CheckLogin)]
        public ActionResult IconList()
        {
            return View(objBizIconInfo.GetList(""));
        }

        void Save(IconInfo objTarget, IconInfo objProvide)
        {
            objTarget.Name = objProvide.Name.CutWord(64);
            objTarget.Icon = objProvide.Icon.CutWord(64);
        }
    }
}
